using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

namespace TestGUI
{
	/// <summary>
	/// This class allows the manager to choose which week he or she wants the 
	/// schedule to be generated for. This is significant because it allows requested
	/// days off to be taken into consideration during schedule generation. 
	/// </summary>
	public class ScheduleWeek : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Label label1;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;
		private System.Windows.Forms.GroupBox groupBox1;
		private System.Windows.Forms.DateTimePicker weekPicker;
		private System.Windows.Forms.Button generateButton;
		private System.Windows.Forms.Button cancelButton;
		Session ses;

		public ScheduleWeek()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		public ScheduleWeek(Session s) 
		{
			
			InitializeComponent();
			ses = s;
		}
	
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			this.Owner.Visible = true;
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.label1 = new System.Windows.Forms.Label();
			this.weekPicker = new System.Windows.Forms.DateTimePicker();
			this.generateButton = new System.Windows.Forms.Button();
			this.cancelButton = new System.Windows.Forms.Button();
			this.groupBox1 = new System.Windows.Forms.GroupBox();
			this.groupBox1.SuspendLayout();
			this.SuspendLayout();
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(8, 16);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(264, 40);
			this.label1.TabIndex = 0;
			this.label1.Text = "Select the Sunday of the week you want to generate this schedule for:";
			this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
			// 
			// weekPicker
			// 
			this.weekPicker.Location = new System.Drawing.Point(16, 64);
			this.weekPicker.Name = "weekPicker";
			this.weekPicker.Size = new System.Drawing.Size(248, 20);
			this.weekPicker.TabIndex = 1;
			this.weekPicker.ValueChanged += new System.EventHandler(this.weekPicker_ValueChanged);
			// 
			// generateButton
			// 
			this.generateButton.Location = new System.Drawing.Point(8, 120);
			this.generateButton.Name = "generateButton";
			this.generateButton.Size = new System.Drawing.Size(136, 24);
			this.generateButton.TabIndex = 2;
			this.generateButton.Text = "Generate";
			this.generateButton.Click += new System.EventHandler(this.generateButton_Click);
			// 
			// cancelButton
			// 
			this.cancelButton.Location = new System.Drawing.Point(152, 120);
			this.cancelButton.Name = "cancelButton";
			this.cancelButton.Size = new System.Drawing.Size(136, 24);
			this.cancelButton.TabIndex = 3;
			this.cancelButton.Text = "Cancel";
			this.cancelButton.Click += new System.EventHandler(this.cancelButton_Click);
			// 
			// groupBox1
			// 
			this.groupBox1.Controls.Add(this.label1);
			this.groupBox1.Controls.Add(this.weekPicker);
			this.groupBox1.Location = new System.Drawing.Point(8, 8);
			this.groupBox1.Name = "groupBox1";
			this.groupBox1.Size = new System.Drawing.Size(280, 96);
			this.groupBox1.TabIndex = 4;
			this.groupBox1.TabStop = false;
			this.groupBox1.Text = "Select Week";
			// 
			// ScheduleWeek
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(292, 150);
			this.Controls.Add(this.groupBox1);
			this.Controls.Add(this.cancelButton);
			this.Controls.Add(this.generateButton);
			this.Name = "ScheduleWeek";
			this.Text = "Select Week";
			this.Load += new System.EventHandler(this.ScheduleWeek_Load);
			this.groupBox1.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

		private void cancelButton_Click(object sender, System.EventArgs e)
		{
			this.Close();
		}

		private void generateButton_Click(object sender, System.EventArgs e)
		{
			ses.generateSchedule(weekPicker.Value);
			Form f = new ScheduleDisplay(ses);
			this.AddOwnedForm(f);
			f.Owner = this;
			f.Visible = true;
			f.Activate();
			this.Visible = false;
		}

		private void weekPicker_ValueChanged(object sender, System.EventArgs e)
		{
			DateTime d = weekPicker.Value;
			if (d.Equals(DayOfWeek.Sunday)) 
			{
				return;
			}
			while (!(d.DayOfWeek.Equals(DayOfWeek.Sunday))) 
			{
				d = d.AddDays(-1);
			}
			weekPicker.Value = d;
		}

		private void ScheduleWeek_Load(object sender, System.EventArgs e)
		{
			DateTime d = weekPicker.Value;
			if (d.Equals(DayOfWeek.Sunday)) 
			{
				return;
			}
			while (!(d.DayOfWeek.Equals(DayOfWeek.Sunday))) 
			{
				d = d.AddDays(-1);
			}
			weekPicker.Value = d;
		}
	}
}
